// pages/testUserInfo/index.js
Page({
  data: {
    loading: false,
    userInfo: null,
    characterInfo: null,
    isNewUser: false,
    openid: '',
    testResults: []
  },

  onLoad() {
    this.getOpenId()
    // 自动获取用户信息
    this.testGetUserInfo()
  },

  // 获取openid
  async getOpenId() {
    try {
      const result = await wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: {
          type: 'getOpenId'
        }
      })

      if (result.result.openid) {
        this.setData({
          openid: result.result.openid
        })
        console.log('当前用户openid:', result.result.openid)
      }
    } catch (error) {
      console.error('获取openid失败:', error)
    }
  },

  // 测试获取用户信息
  async testGetUserInfo() {
    this.setData({ loading: true })
    
    try {
      const startTime = Date.now()
      const result = await wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: {
          type: 'getUserInfo'
        }
      })
      const endTime = Date.now()
      const duration = endTime - startTime

      const testResult = {
        timestamp: new Date().toLocaleTimeString(),
        duration: duration + 'ms',
        success: result.result.success,
        isNewUser: result.result.isNewUser,
        data: result.result.data,
        error: result.result.errMsg || null
      }

      this.setData({
        userInfo: result.result.data?.user || null,
        characterInfo: result.result.data?.character || null,
        isNewUser: result.result.isNewUser,
        testResults: [testResult, ...this.data.testResults].slice(0, 10) // 保留最近10次测试
      })

      console.log('获取用户信息结果:', result.result)
      console.log('用户信息:', result.result.data?.user)
      console.log('角色信息:', result.result.data?.character)
      
      if (result.result.success) {
        if (result.result.isNewUser) {
          wx.showToast({
            title: '新用户，需要创建',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '获取成功',
            icon: 'success'
          })
        }
      } else {
        wx.showToast({
          title: result.result.errMsg || '获取失败',
          icon: 'none'
        })
      }
    } catch (error) {
      console.error('测试获取用户信息失败:', error)
      wx.showToast({
        title: '测试失败',
        icon: 'none'
      })
      
      const testResult = {
        timestamp: new Date().toLocaleTimeString(),
        duration: 'N/A',
        success: false,
        isNewUser: false,
        data: null,
        error: error.message
      }
      
      this.setData({
        testResults: [testResult, ...this.data.testResults].slice(0, 10)
      })
    } finally {
      this.setData({ loading: false })
    }
  },

  // 清空测试记录
  clearTestResults() {
    this.setData({
      testResults: []
    })
  },

  // 复制用户信息到剪贴板
  copyUserInfo() {
    if (this.data.userInfo) {
      const userInfoStr = JSON.stringify(this.data.userInfo, null, 2)
      wx.setClipboardData({
        data: userInfoStr,
        success: () => {
          wx.showToast({
            title: '用户信息已复制',
            icon: 'success'
          })
        }
      })
    }
  },

  // 复制角色信息到剪贴板
  copyCharacterInfo() {
    if (this.data.characterInfo) {
      const characterInfoStr = JSON.stringify(this.data.characterInfo, null, 2)
      wx.setClipboardData({
        data: characterInfoStr,
        success: () => {
          wx.showToast({
            title: '角色信息已复制',
            icon: 'success'
          })
        }
      })
    }
  },

  // 复制openid到剪贴板
  copyOpenId() {
    if (this.data.openid) {
      wx.setClipboardData({
        data: this.data.openid,
        success: () => {
          wx.showToast({
            title: 'OpenID已复制',
            icon: 'success'
          })
        }
      })
    }
  },

  // 导航到个人信息页面
  navigateToProfile() {
    wx.navigateTo({
      url: '/pages/userProfile/index'
    })
  }
})
